package com.kuzufang.modules.search;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kuzufang.R;
import com.kuzufang.base.BaseAdapter;
import com.kuzufang.utils.DisplayUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by songyuanjin on 2017/9/22.
 */

public class SubWayStationAdapter extends BaseAdapter<String, RecyclerView.ViewHolder> {
    private String[] subStations = new String[]{
            "莘庄",
            "外环路",
            "莲花路",
            "锦江乐园",
            "上海南站",
            "漕宝路",
            "上海体育馆",
            "徐家汇",
            "衡山路",
            "常熟路",
            "陕西南路",
            "黄陂南路",
            "人民广场",
            "新闸路",
            "汉中路",
            "上海火车站",
            "中山北路",
            "延长路",
            "上海马戏城",
            "汶水路",
            "彭浦新村",
            "共康路",
            "通河新村",
            "呼兰路",
            "共富新村 ",
            "宝安公路",
            "友谊西路",
            "富锦路"
    };

    private static final int TOP = 1;
    private static final int BOTTOM = 2;

    @Override
    public int getItemViewType(int position) {
        if (position % 2 == 0) {
            return TOP;
        } else {
            return BOTTOM;
        }
    }

    public SubWayStationAdapter(Context ctx) {
        super(ctx);
        List<String> strings = new ArrayList<>();
        for (int i = 0; i < subStations.length; i++) {
            strings.add(subStations[i]);
        }
        clear();
        addAll(strings);
        notifyDataSetChanged();
    }

    @Override
    protected void bindViewHolderData(RecyclerView.ViewHolder viewHolder, String data, int position) {
        int type = getItemViewType(position);
        switch (type) {
            case TOP:
                ((SubWayStationAdapter.SubWayHolder) viewHolder).bindData(data, position);
                break;
            case BOTTOM:
                ((SubWayStationAdapter.SubWayBottomHolder) viewHolder).bindData(data);
                break;
        }

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case TOP:
                View viewTop = LayoutInflater.from(mCtx).inflate(R.layout.item_subway_station_top, parent, false);
                return new SubWayHolder(viewTop);
            case BOTTOM:
                View viewBottom = LayoutInflater.from(mCtx).inflate(R.layout.item_subway_station_bottom, parent, false);
                return new SubWayBottomHolder(viewBottom);
        }

        return null;
    }

    class SubWayHolder extends RecyclerView.ViewHolder {
        private TextView mSubWayLineTextView;
        private RelativeLayout itemTopView;

        public SubWayHolder(View itemView) {
            super(itemView);
            itemTopView = (RelativeLayout) itemView.findViewById(R.id.rl_top_view);
            mSubWayLineTextView = (TextView) itemView.findViewById(R.id.tv_subway_station);
        }

        public void bindData(String data, int position) {
            mSubWayLineTextView.setText(data);
            if ((position != 0) && (position % 2 == 0)) {
                RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) itemTopView.getLayoutParams();
                layoutParams.leftMargin = DisplayUtils.dp2px(-30);
                itemTopView.setLayoutParams(layoutParams);
            } else {
                RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) itemTopView.getLayoutParams();
                layoutParams.leftMargin = 0;
                itemTopView.setLayoutParams(layoutParams);
            }
        }
    }

    class SubWayBottomHolder extends RecyclerView.ViewHolder {
        private TextView mSubWayLineTextView;


        public SubWayBottomHolder(View itemView) {
            super(itemView);
            mSubWayLineTextView = (TextView) itemView.findViewById(R.id.tv_subway_station_bottom);
        }

        public void bindData(String data) {
            mSubWayLineTextView.setText(data);
        }
    }


}
